﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

namespace _9._1
{  
    public partial class Form1 : Form
    {

        private string url;
        public Form1()
        {
            InitializeComponent();
            url = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            url = textBox1.Text;
            richTextBox1.Text = "";
            if (url == "")
            {
                MessageBox.Show("请输入网址");
                return;
            }
            try
            {
                WebRequest request = WebRequest.Create(this.url);
                WebResponse response = request.GetResponse();
                Stream strm = response.GetResponseStream();
                StreamReader sr = new StreamReader(strm);
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    MatchCollection mc = Regex.Matches(line, @"\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}");
                    foreach (Match match in mc)
                    {
                        richTextBox1.Text += match.ToString() + "\n";
                        //.WriteLine(match.ToString());
                    }
                }
                strm.Close();
                if (richTextBox1.Text == "")
                {
                    richTextBox1.Text = "未查找到邮箱";
                }
            }
            catch(Exception)
            {
                MessageBox.Show("检查网址是否合法");
                return;
            }
        }
    }
}
